﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FWork
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (textBox1.Text != null && textBox1.Text != "")
            {
                OpenFileDialog d = new OpenFileDialog();
                d.Filter = "All files|*.*";
                d.Multiselect = true;
                if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (string filePath in d.FileNames)
                    {
                        //System.Diagnostics.Debug.WriteLine(filePath);
                        
                        string fileName = filePath.Remove(0, filePath.LastIndexOf('\\')+1);
                        //System.Diagnostics.Debug.WriteLine(fileName);

                        if (System.IO.File.Exists(filePath))
                        {
                            int p = fileName.LastIndexOf('.');
                            if (p != -1 && p != 0 )
                            {
                                System.IO.File.Move(filePath, filePath.Remove(filePath.LastIndexOf('.') + 1) + textBox1.Text);
                            }
                            else
                            {
                                System.IO.File.Move(filePath, filePath + '.' + textBox1.Text);
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入后缀名");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string Path = "";
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == DialogResult.OK)
            {
                Path = folder.SelectedPath;
                System.Diagnostics.Debug.WriteLine(Path);
            }
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void button3_Click_1(object sender, EventArgs e)
        {

            FolderBrowserDialog d = new FolderBrowserDialog();
            if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var files = System.IO.Directory.GetDirectories(d.SelectedPath);

                foreach (string file in files)
                {

                    string filePath = file;


                    //System.Diagnostics.Debug.WriteLine(filePath);

                    int fileIndex = filePath.LastIndexOf('\\') + 1;
                    string fileName = filePath.Remove(0, fileIndex);
                    //System.Diagnostics.Debug.WriteLine(fileName);
                        
                    if (System.IO.Directory.Exists(filePath))
                    {
                        //System.Diagnostics.Debug.WriteLine('1');
                        while (fileName[0] == '(' || fileName[0] == '[' || fileName[0] == '（' || fileName[0] == '【' || fileName[0] == '{' || fileName[0] == ' ')
                        {
                            if (fileName[0] == '(')
                            {
                                int p = fileName.IndexOf(')') + 1;
                                if (p != -1 && p != 0)
                                {
                                    if (p == fileName.Length)
                                        break;
                                    fileName = fileName.Remove(0, p);
                                    if (System.IO.Directory.Exists(filePath.Remove(fileIndex, p)))
                                    {
                                        int num = 1;
                                        while (System.IO.Directory.Exists(filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")"))
                                        {
                                            num++;
                                        }
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")");
                                    }
                                    else
                                    {
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p));
                                    }
                                    filePath = filePath.Remove(fileIndex, p);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else if (fileName[0] == '[')
                            {
                                int p = fileName.IndexOf(']') + 1;
                                if (p != -1 && p != 0)
                                {
                                    if (p == fileName.Length)
                                        break;
                                    fileName = fileName.Remove(0, p);
                                    if (System.IO.Directory.Exists(filePath.Remove(fileIndex, p)))
                                    {
                                        int num = 1;
                                        while (System.IO.Directory.Exists(filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")"))
                                        {
                                            num++;
                                        }
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")");
                                    }
                                    else
                                    {
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p));
                                    }
                                    filePath = filePath.Remove(fileIndex, p);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else if (fileName[0] == '（')
                            {
                                int p = fileName.IndexOf('）') + 1;
                                if (p != -1 && p != 0)
                                {
                                    if (p == fileName.Length)
                                        break;
                                    fileName = fileName.Remove(0, p);
                                    if (System.IO.Directory.Exists(filePath.Remove(fileIndex, p)))
                                    {
                                        int num = 1;
                                        while (System.IO.Directory.Exists(filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")"))
                                        {
                                            num++;
                                        }
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")");
                                    }
                                    else
                                    {
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p));
                                    }
                                    filePath = filePath.Remove(fileIndex, p);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else if (fileName[0] == '【')
                            {
                                int p = fileName.IndexOf('】') + 1;
                                if (p != -1 && p != 0)
                                {
                                    if (p == fileName.Length)
                                        break;
                                    fileName = fileName.Remove(0, p);
                                    if (System.IO.Directory.Exists(filePath.Remove(fileIndex, p)))
                                    {
                                        int num = 1;
                                        while (System.IO.Directory.Exists(filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")"))
                                        {
                                            num++;
                                        }
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")");
                                    }
                                    else
                                    {
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p));
                                    }
                                    filePath = filePath.Remove(fileIndex, p);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else if (fileName[0] == '{')
                            {
                                int p = fileName.IndexOf('}') + 1;
                                if (p != -1 && p != 0)
                                {
                                    if (p == fileName.Length)
                                        break;
                                    fileName = fileName.Remove(0, p);
                                    if (System.IO.Directory.Exists(filePath.Remove(fileIndex, p)))
                                    {
                                        int num = 1;
                                        while (System.IO.Directory.Exists(filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")"))
                                        {
                                            num++;
                                        }
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p) + "(" + num.ToString() + ")");
                                    }
                                    else
                                    {
                                        System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, p));
                                    }
                                    filePath = filePath.Remove(fileIndex, p);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else if (fileName[0] == ' ')
                            {

                                if (fileName.Length <= 1)
                                    break;
                                fileName = fileName.Remove(0, 1);
                                if (System.IO.Directory.Exists(filePath.Remove(fileIndex, 1)))
                                {
                                    int num = 1;
                                    while (System.IO.Directory.Exists(filePath.Remove(fileIndex, 1) + "(" + num.ToString() + ")"))
                                    {
                                        num++;
                                    }
                                    System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, 1) + "(" + num.ToString() + ")");
                                }
                                else
                                {
                                    System.IO.Directory.Move(filePath, filePath.Remove(fileIndex, 1));
                                }
                                filePath = filePath.Remove(fileIndex, 1);

                            }
                        }

                    }
                }
            }
        }
    }
}
